mui.init();


mui.plusReady(function(){
	loaddataSerucrity();
	var name = plus.storage.getItem(KeyCfg.userName);
	$("#name").val(name);
	
	var phone_answer = CmwMcore.$('phone_answer');
	MeventMgr.onTouchstart(phone_answer,function(event,self){
		var a= document.getElementById("a");
		a.checked=true;
		$(".qcode").show();
		$(".emailpwd").hide();
		$(".resetypwd").hide();
		$(".qypwd").hide();
	});
	
	var email_card = CmwMcore.$('email_card');
	MeventMgr.onTouchstart(email_card,function(event,self){
		var b= document.getElementById("b");
		b.checked=true;
		$(".qcode").hide();
		$(".emailpwd").show();
		$(".resetypwd").hide();
		$(".qypwd").hide();
	});
	
	var answer_ypwd = CmwMcore.$('answer_ypwd');
	MeventMgr.onTouchstart(answer_ypwd,function(event,self){
		var c= document.getElementById("c");
		c.checked=true;
		$(".qcode").hide();
		$(".emailpwd").hide();
		$(".resetypwd").hide();
		$(".qypwd").show();
	});
	
	/**------- 安全问题+手机验证码  -------*/
	var btn_qphone =CmwMcore.$('btn_qphone');
	MeventMgr.onTouchstart(btn_qphone,function(event,self){
		if(!valid()) return;
		submitForm();
	});
	
	
	/**------- 邮箱验证码+身份证  -------*/
	var btn_emailpwd = CmwMcore.$('btn_emailpwd');
	MeventMgr.onTouchstart(btn_emailpwd,function(event,self){
		if(!valid1()) return;
		submitForm1();
	});
	
	/**------- 安全问题+原交易密码  -------*/
	var btn_qypwd = CmwMcore.$('btn_qypwd');
	MeventMgr.onTouchstart(btn_qypwd,function(event,self){
		if(!valid2()) return;
		submitForm2();
	});
	
	
	/**------- 提交新交易密码  -------*/
	var btn_resetypwd = CmwMcore.$('btn_resetypwd');
	MeventMgr.onTouchstart(btn_resetypwd,function(event,self){
		if(!valid3()) return;
		submitForm3();
	});
	
	function submitForm(){
		var answer1 = $("#answer1").val();
		var answer2 = $("#answer2").val();
		var questionone = $("#questionone").val();
		var questiontwo = $("#questiontwo").val();
		var phone = $("#phonecode").val();
		var smstype="validPassword";
		var pars={answer1:answer1,answer2:answer2,questionone:questionone,questiontwo:questiontwo,phone:phone,smstype:smstype};
		var url =UrlConfig.validSafeAnswer;
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		var status = data.status;
		  		if (status == "true") {
					mui.alert("验证成功");
					$(".qbox").hide();
					$(".qcode").hide();
					$(".emailpwd").hide();
					$(".resetypwd").show();
					$(".qypwd").hide();
				} else if (status == "errorphone") {
					mui.alert("手机验证码错误");					
				} else {
					mui.alert("安全问题回答错误");					
				}
	        		
		  	},ffn : function(err){
		  		mui.alert("获取数据失败：["+err+"]");
		  	}
		});
	}
	
	
	function submitForm1(){
		var card = $("#card1").val();
		var emailcode =$("#emailcode").val();
		var url =UrlConfig.checkEmailCard;
		var pars={card:card,emailcode:emailcode};
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		var status = data.status;
		  		if (status == "true") {
                	mui.alert("验证成功");
                	$(".qbox").hide();
                    $(".qcode").hide();
					$(".emailpwd").hide();
					$(".resetypwd").show();
					$(".qypwd").hide();
                } else if (status == "errorCard") {
                	mui.alert("身份证号码错误");                	
                } else if (status == "errorEmail") {
                	mui.alert("邮箱验证码错误");            	
                } else {
                	mui.alert("验证失败");               	
                }
	        		
		  	},ffn : function(err){
		  		mui.alert("获取数据失败：["+err+"]");
		  	}
		});
	}
	
	function valid(){
		var answer1_val = $("#answer1").val();
		var answer2_val =$("#answer2").val();
		var phonecode_val =$("#phonecode").val();
		if(!answer1_val || !answer1_val.trim()){
			mui.alert("问题一不能为空!");
			return false;
		}
		if(!answer2_val || !answer2_val.trim()){
			mui.alert("问题二不能为空!");
			return false;
		}
		if(!phonecode_val || !phonecode_val.trim()){
			mui.alert("验证码不能为空!");
			return false;
		}
		
		return true;
	}
	
	
	function valid2(){
		var answer1_val = $("#answer3").val();
		var answer2_val =$("#answer4").val();
		var ypwd_val =$("#ypwd").val();		
		if(!answer1_val || !answer1_val.trim()){
			mui.alert("问题一不能为空!");
			return false;
		}
		if(!answer2_val || !answer2_val.trim()){
			mui.alert("问题二不能为空!");
			return false;
		}
		if(!ypwd_val || !ypwd_val.trim()){
			mui.alert("原交易密码不能为空!");
			return false;
		}
		
		return true;
	}
	
	
	function valid3(){
		
		var tranpwd_val = $("#newypwd").val();
		var tranpwdconf_val = $("#tranypwd").val();
		if(!tranpwd_val){
			mui.alert("请输入交易密码!");
			return false;
		}
		
		var usern = /^(?=[0-9a-zA-Z]+\d)(?=[0-9a-zA-Z]+[a-zA-Z])(?=[0-9a-zA-Z]).{8,16}$/;
		if((tranpwd_val.length<8 || tranpwd_val.length>16) ||
		!usern.test(tranpwd_val)){
			$("#tranpwd").focus();
			mui.alert("交易密码：请输入8-16位字母加数字的组合!");
			return false;
		}
		
		if(!tranpwdconf_val){
			mui.alert("请输入确定密码!");
			return false;
		}
		
		if(tranpwd_val != tranpwdconf_val){
			mui.alert("确定密码必须与交易密码相同!");
			return false;
		}
		
		return true;
	}
	
	
	function valid1(){
		var cardid_val = $("#card1").val();
		var emailcode_val =$("#emailcode").val();
		
		if(!cardid_val || !cardid_val.trim()){
			mui.alert("请输入您的身份证号!");
			return false;
		}
		
		if(!emailcode_val || !emailcode_val.trim()){
			mui.alert("请输入验证码!");
			return false;
		}
		
		var statuss = false;
		card = cardid_val.trim();
		if (card.length == 15 || card.length == 18) {
			//中国身份证 15位或者18位
			statuss = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(card);
		} else if (/(^[A-Z]$)/.test(card.substr(0, 1))) {
			//香港身份证以字母开头，只验证是否以字母开头，不验证长度
			statuss = true;
		} else if (card.length == 8) {
			//港澳身份证
			statuss = /(^[A-Z0-9]\d{6}[A-Z0-9]$)/.test(card);
		}
		if(!statuss){
			mui.alert("身份证号码格式错误!");
			return false; 
		}
		return true;
	}
	
	 //验证安全问题和原交易 密码
    function submitForm2() {
		var questionone = $("#questionone3").val();
		var questiontwo = $("#questiontwo4").val();
        var answer1_val = $("#answer3").val();
		var answer2_val = $("#answer4").val();
		var ypwd = $("#ypwd").val();		
		var url =UrlConfig.checkPassSercurity;
		var pars ={ypwd:ypwd,answer1:answer1_val,answer2:answer2_val,questionone:questionone,questiontwo:questiontwo};
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		var status = data.status;
		  		if (status == "true") {
					mui.alert("验证成功");
					$(".qbox").hide();
					$(".qcode").hide();
					$(".emailpwd").hide();
					$(".resetypwd").show();
					$(".qypwd").hide();
				} else if (status == "errordealpass") {
					mui.alert("原交易密码错误!");
					
				} else {
					mui.alert("安全问题回答错误!");
				}
	        		
		  	},ffn : function(err){
		  		mui.alert("获取数据失败：["+err+"]");
		  	}
		});


    }
	
	 //提交修改密码
    function submitForm3() {
        var transPassword = $("#newypwd").val();
		var transPassword2 = $("#tranypwd").val();
		var ypwd = $("ypwd");			
		var url =UrlConfig.updateDealPassword;
		var pars ={transPassword:transPassword};
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		var status = data.status;
		  		if (status == "true") {
		  			mui.alert("修改成功");	
		  			mui.back();
				} else if (status == "xt") {
					mui.alert("交易密码和登录密码不能相同");
				} else if(status =="yxt"){
					mui.alert("新交易密码不能与原交易密码相同");
				}else{
					mui.alert("修改失败");
				}
	        		
		  	},ffn : function(err){
		  		mui.alert("获取数据失败：["+err+"]");
		  	}
		});
    }
    
    
});


//function checkpwd(para) {
//	// 密码 先判断
//	var j = 0;
//	var shuzi = 0;
//	var daxie = 0;
//	var xiaoxie = 0;
//	var other = 0;
//	var pwd_length = para.length;
//	if (para.length != 0) {
//		if (!(pwd_length >= 8 && pwd_length <= 16)) {
//			return false;
//		}
//		for (var i = 0; i < para.length; i++) {
//			var a = para[i];
//			var testReg1 = /[0-9]/g; // 数字
//			var testReg2 = /[A-Z]/g; // 大写字母
//			var testReg3 = /[a-z]/g;// 小写字母
//			if (testReg1.test(a)) {
//				shuzi++;
//			} else if (testReg2.test(a)) {
//				daxie++;
//			} else if (testReg3.test(a)) {
//				xiaoxie++;
//			}else{
//				other++;
//			}
//		}
//		if(other > 0){
//			return false;
//		}
//		if (shuzi > 0) {
//			j++;
//		}
//		if (daxie + xiaoxie > 0) {
//			j++;
//		}
//		if (j >= 2) {
//			// 密码符合要求
//			return true;
//		} else {
//			return false;
//		}
//	}
//}
//  
//function checkJspUse(jypassword, rejypassword){
//	if(!checkpwd(jypassword)) {
//		return false;
//	}
//	if(rejypassword =='') {
//		return false;
//	} else if(jypassword!=rejypassword){
//		return false;
//	}else {
//		return true;
//	}
//}


function loaddataSerucrity(){
	var url = UrlConfig.getSerucrity
	var guid = plus.storage.getItem(KeyCfg.userId);
	var pars={guid:guid};
	// 弹出系统等待对话框   
	//var w = mui.plus.nativeUI.showWaiting( "正在加载..." );
	MuiEventMgr.ajax(url,{pars:pars,
	  	sfn : function(data){
	  		var list = data.list;
	  		var answer1 = list[0];
	  		var answer2 = list[1];
	  		var question1 =answer1.problem;
	  		var question2 =answer2.problem;
	  		$("#questionone").val(question1);
	  		$("#questiontwo").val(question2);
	  		$("#questionone3").val(question1);
	  		$("#questiontwo4").val(question2);	  		
	  	},ffn : function(err){
	  		mui.alert("获取数据失败：["+err+"]");
	  		//mui.plus.nativeUI.closeWaiting();
	  	}
	 });
	
}
	

